package com.example.controller;

import com.example.Repository.UserRepository;
import com.example.domain.property.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @PostMapping("/login")
    @ResponseBody
    public User login(@RequestBody User user, HttpSession session){
        User res=userService.checkUser(user.getUsername(),user.getPassword());
        if(res!=null){
            res.setPassword(null);
            res.setBlogList(null);
            session.setAttribute("user",res);
        }
        return res;
    }
    //首页获取数据用户数据
    @GetMapping("/gainData")
    @ResponseBody
    public User gainData(HttpSession session){
        User user=(User)session.getAttribute("user");
        return user;
    }
    //注销
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/back/login/login.html";
    }
}
